<html lang="en">
<head>
    <meta charset="UTF-8">
</head>

<body>
<h1>监听测试页面-模拟监听客户端</h1>
<button onclick="setMessageContent('鼠标点我了')">测试message展示</button>
<button onclick="connect()">测试链接</button>
<div id="message">展示服务的推送过来消息的地方</br></div>

<button onclick="sourceClose()">关闭连接</button>

<script>
    id = "123";
    source = null;

    url = "http://172.16.0.173:9000";
    local = "http://localhost:9000";


    // function connect() {
    //     source = new EventSource(url + "/connect?id="+id);
    // }

    if(window.EventSource){
        console.info("此浏览器支持SSE");
        // 连接的建立
        source = new EventSource(url + "/connect?id="+id);

        // 监听服务的推送的消息
        source.addEventListener("message",function (e){
            content = e.data;
            console.log("消息内容："+e.data);

            setMessageContent(content);
        });

        // source.addEventListener("error", function (e) {
        //     console.log("服务器error数据为:" + e.data);
        // });
    }else {
        setMessageContent("此浏览器不支持");
    }


    function setMessageContent(content){
        document.getElementById("message").innerHTML+= (content+'</br>');
    }


</script>
</body>
</html>